
function delete_article(aid) {
    if (!confirm("确认要删除该文章吗？")){
        return;
    }
    $.ajax({
        url: "/admin/article/delete/"+aid,
        type: "POST",
        contentType: "json",
        success: function (data) {
            console.log(data)
            if (data.code === 1){
                alert(data.msg);
                window.location.href = "/admin/article";
            }else{
                alert(data.msg);
            }
        },
        error: function () {
            alert("删除失败！");
        }
    })
}

var convert_FormData_to_json = function (formData) {
    var objData = {};

    for (var entry of formData.entries()){
        objData[entry[0]] = entry[1];
    }
    return JSON.stringify(objData);
};

/**
 * 提交文章
 */
function submit_article(status) {
    var title = $('#article-title').val();
    var content = $('#editor').val();   // 获取编辑器的内容
    if (title === '') {
        alert("请输入文章标题");
        return;
    }
    if (content === '') {
        alert("文章内容为空，请输入文章内容");
        return;
    }
    var formData = new FormData();
    formData.append("title", title);
    formData.append("content", content);
    formData.append("tags", $('#article-tags').val());
    formData.append("type", $('#multiple-type').val());
    if (status===0){    // 如果是草稿就设置为0
        formData.append("status", status);
    }else{
        formData.append("status", $('#multiple-sel').val());
    }
    var aid = $('#article-aid').val();
    var url = '/admin/article/publish';
    if (aid !== ''){
        formData.append("aid", aid);
        url = '/admin/article/update';
    }

    $.ajax({
        url: url,
        type: "POST",
        // contentType: "json",
        data: formData,
        processData: false,       //必不可缺
        contentType: false,       //必不可缺
        // dataType: "json",
        success: function (result) {
            if (result.code === 1) {
                alert(result.msg);  // 打印返回消息
                window.location.href = '/admin/article';
            } else {
                alert(result.msg);
            }
        },
        error: function () {
            alert("服务端错误！");
        }
    })
}